package com.liu.controller;

import com.liu.pojo.Result;
import com.liu.pojo.User;
import com.liu.service.UserService;
import com.liu.utils.JwtUtil;
import com.liu.utils.Md5Util;
import com.liu.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Validated
@RequestMapping("/user")
public class UserController {

    @Autowired
    public UserService userService;

    @PostMapping("/register")
    public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {
        User user = userService.findByUserName(username);
        if (user == null) {
            userService.register(username, password);
            return Result.success();
        } else {
            return Result.error("用户名已被占用！");
        }
    }

    @PostMapping("/login")
    public Result<String> login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {
        User loginUser = userService.findByUserName(username);
        if (loginUser == null) {
            return Result.error("用户名错误！");
        }
        if (Md5Util.getMD5String(password).equals(loginUser.getPassword())) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginUser.getUsername());
            claims.put("username", loginUser.getPassword());
            String token = JwtUtil.genToken(claims);
            return Result.success(token);
        }
        return Result.error("密码错误！");
    }

    @GetMapping("/userInfo")
    public Result<User> userInfo(/*@RequestHeader(name = "Authorization") String token*/) {
//        从请求头中获取登录成功的用户名，进行该用户的信息查询。
        /*Map<String, Object> map = JwtUtil.parseToken(token);
        String username = (String) map.get("username");*/
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userService.findByUserName(username);
        return Result.success(user);
    }

    @PostMapping("/updata")
    public Result updata(@RequestBody @Validated User user) {
        userService.updata(user);
        return Result.success();
    }

    @PatchMapping("/updataAvatar")
    public Result updataAvatar(@RequestParam("avatar") @URL String avatarUrl) {
        userService.updataAvatar(avatarUrl);
        return Result.success();
    }

    @PatchMapping("/updataPwd")
    public Result updataPwd(@RequestBody Map<String, String> params){
//        1、校验参数
        String oldPwd = params.get("old_pwd");
        String newPwd = params.get("new_pwd");
        String rePwd = params.get("re_pwd");

        if (!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) ||!StringUtils.hasLength(rePwd)){
            return Result.error("参数不能为空");
        }

//        校验新密码和确认密码是否一致
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User loginUser = userService.findByUserName(username);
        if (!loginUser.getPassword().equals(Md5Util.getMD5String(oldPwd))){
            return Result.error("旧密码错误");
        }

        if (!rePwd.equals(newPwd)){
            return Result.error("两次密码不一致");
        }

//        2、调用service层方法进行修改
        userService.updataPwd(newPwd);
        return Result.success();

    }

}


